package com.zhuiYun.project.api.dynamicComment.service.serviceImpl;

import com.zhuiYun.project.api.dynamicComment.entity.Comment;
import com.zhuiYun.project.api.dynamicComment.mapper.CommentMapper;
import com.zhuiYun.project.api.dynamicComment.service.CommentService;
import com.zhuiYun.project.util.pageUtil.MybatisPageHelper;
import com.zhuiYun.project.util.pageUtil.PageRequest;
import com.zhuiYun.project.util.pageUtil.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CommentService
 * @Description 评论service
 * @Author WJ
 * @Date 2023/4/2 17:23
 **/
@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    CommentMapper commentMapper;
    /**
     * @Author WJ
     * @Description 保存评论
     * @Date 2023/4/2 22:55
     * @Return int
     * @Param [comment]
     **/
    public int saveComment(Comment comment) {
        comment.setCommentTime(new Date());
        int i = commentMapper.insertSelective(comment);
        return i;
    }
    /**
     * @Author WJ
     * @Description 回复评论
     * @Date 2023/4/5 7:28
     * @Return int
     * @Param [comment]
     **/
    @Override
    public int saveReply(Comment comment) {
        comment.setCommentTime(new Date());
        int i = commentMapper.insertSelective(comment);
        return i;
    }
    /**
     * @Author WJ
     * @Description 查询全部评论
     * @Date 2023/4/3 17:43
     * @Return com.zhuiYun.project.util.pageUtil.PageResult
     * @Param [pageResult,divId]
     **/
    @Override
    public PageResult getAllCommentByDivId(PageRequest pageRequest, Integer divId) {
        MybatisPageHelper.startPage(pageRequest);
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("divId",divId);
        // 获取全部的评论
        List<Comment> allComment = commentMapper.selectByExample(example);
        // 根据流的形式获取所有的跟评论
        List<Comment> rootComments = allComment.stream().filter(comment -> comment.getReplyUserId() == null).collect(Collectors.toList());
        // 遍历所有的根评论
        for(Comment rootComment: rootComments){
            // 对根评论遍历出回复的评论
            rootComment.setChildren(allComment.stream().filter(comment -> rootComment.getUserId() == comment.getReplyUserId()).collect(Collectors.toList()));
        }
        PageResult result = MybatisPageHelper.getPageResult(rootComments);
        return result;
    }
    /**
     * @Author WJ
     * @Description 删除评论及回复
     * @Date 2023/4/6 20:33
     * @Return int
     * @Param [id]
     **/
    @Override
    public int deleteCommentById(int id) {
        Example example = new Example(Comment.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id",id);
        return commentMapper.deleteByPrimaryKey(id);
    }
}
